package com.yanqu.road.server.cmd.activity.stronghold.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.enums.activity.stronghold.eStrongholdEventType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_CROSS_STRONGHOLD_WILD_SEARCH, desc = "野外据点搜索返回")
public class CrossStrongholdSearchCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 返回给客户端
        ActivityInfo openActivityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (openActivityInfo != null) {
            StrongholdWarProto.StrongholdSearchRespMsg.Builder resp = StrongholdWarProto.StrongholdSearchRespMsg.newBuilder();
            StrongholdWarProto.CrossStrongholdSearchRespMsg req = StrongholdWarProto.CrossStrongholdSearchRespMsg.parseFrom(packet.getBytes());
            resp.setRet(req.getRet());
            player.getModule(StrongholdModule.class).setEventConsume(openActivityInfo.getActivityId(), null);
            if (req.hasEvent()) {
                resp.setEvent(req.getEvent());
                player.getModule(StrongholdModule.class).reducePower(StrongholdActivityMgr.getSearchPower(),1);
                StrongholdWarProto.StrongholdEventTempMsg event = req.getEvent();
                SeasonTaskConditionData conditionData = new SeasonTaskConditionData(BigInteger.valueOf(openActivityInfo.getType()),BigInteger.valueOf(StrongholdActivityMgr.getSearchPower()));
                player.notifyListener(eGamePlayerEventType.UnionFightCostStaminaTask.getValue(), conditionData);
                if (event.getType() == eStrongholdEventType.NPC.getValue()) {
                    String consume = event.getNpcEvent().getConsume();
                    player.getModule(StrongholdModule.class).setEventConsume(openActivityInfo.getActivityId(), consume);
                }
            }
            player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_WILD_SEARCH, resp);
        }

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
